#!/usr/bin/perl
# mod by Marcelo Licastro Pagni

use strict;
use LWP::UserAgent;
use Getopt::Long;
use Sys::Hostname;
use XML::DOM;

my ($content, $service, $item, $monitor, $value, $response, $match, $verbose, $help, $uri);
my $hostname  = hostname;
my $timestamp = time();
my $match = "true";
my $status = 0;

$uri     = "http://quebec.api.srv.intranet/quebec-api/carrier";    # Glete
#$uri     = "http://quebec.adm.intranet/quebec-api/carrier";        # Barao:
#$uri       = "http://192.168.128.140/quebec-api/carrier";            # Staging:

usage() if ( @ARGV < 8 or
        ! GetOptions('help|?'       => \$help,
                    'service=s'     => \$service,
                    'item=s'        => \$item,
                    'monitor=s'     => \$monitor,
                    'value=s'       => \$value,
                    'match:s'       => \$match,
                    'status:i'      => \$status,
                    'verbose!'      => \$verbose)
            or defined $help );


if (($match eq "true") && ( $value  !~ /^[-0-9]+$/ )) {
    print "\nMatch=true, value MUST be a number!\n";
    usage();
    exit -1;
}

&createContent();

sub createContent
{
    my $doc = XML::DOM::Document->new;
    my $xml_pi = $doc->createXMLDecl('1.0', 'UTF-8');
    my $root = $doc->createElement('carrierList');
    $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    $root->setAttribute('xsi:noNamespaceSchemaLocation', 'quebec_carrier.xsd');
    my $carrier=$doc->createElement('carrier');
    $root->appendChild($carrier);
    my @leafs2 =    (
                        ["hostname",    "$hostname"],
                        ["service",     "$service"],
                        ["item",        "$item"],
                        ["monitor",     "$monitor"],
                        ["value",       "$value"],
                        ["timestamp",   "$timestamp"],
                        ["match",       "$match"],
                        ["status",      "$status"]
                    );

    my $leaf;
    my $leaf_text;
    for my $i ( 0 .. $#leafs2 ) {
        $leaf = $doc->createElement($leafs2[$i][0]);
        $leaf_text = $doc->createTextNode($leafs2[$i][1]);
        $leaf->appendChild($leaf_text);
        $carrier->appendChild($leaf);
    }
    my $content_xml_dom = $xml_pi->toString . "\n" . $root->toString . "\n";
    if ($verbose) { print $content_xml_dom; }
    &sendContent($content_xml_dom);

}

sub sendContent{
    if ($verbose) { print "\nTrying to send to gateway...\n\n"; }
    my $header    = HTTP::Request->new(Accept => 'application/xml');
    my $request   = HTTP::Request->new("POST",$uri,$header,@_);
    my $useragent = LWP::UserAgent->new;
    $useragent->timeout(15);
    my $res = $useragent->request($request);
    if ($res->is_success) {
        if ($verbose) { print $res->as_string ."\n"; }
        exit 0;
    } else {
        if ($verbose) { print $res->as_string . "\n"; }
        exit 1;
    }
}

sub usage
{
    print "\nUsage: $0 --service <servico> --item <item> --monitor <monitor> --value <valor> [--match true|false] [--status 0|1|2|3] [--verbose]\n\n" ;
    print "Defaults:\n";
    print "\tmatch => true\n";
    print "\tstatus => 0\n";
    print "\tverbose is disabled\n\n";
    exit -1;
}
